/**
  * @(#)LogManager.java
  *
  * Provide the abstract implementation of the LogManager to 
  * manage the creation and usage of the log files.
  *
  * @author Course Scheduler Team
  * 
  * @license GNU General Public License version 3 (GPLv3)
  *
  * This file is part of Course Scheduler, an open source, cross platform
  * course scheduling tool, configurable for most universities.
  *
  * Copyright (C) 2010-2012 Course Scheduler Team
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * 
  */
package com.pollicitus.scheduler.logging;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.slf4j.Logger;

import com.pollicitus.scheduler.core.Manager;
import com.pollicitus.scheduler.core.NamespaceSettings;
import com.pollicitus.scheduler.core.Settings;
import com.pollicitus.scheduler.logging.LogManager;

/**
 * Provide the abstract implementation of the LogManager to 
  * manage the creation and usage of the log files.
 * 
 * @author Course Scheduler Team
 *
 */
public abstract class LogManager extends Manager {
	
	//protected class constants
	protected static final String NAMESPACE = "logging";
	
	//public static variables
	public static LogManager logManager;
		
	/**
	 * Return the log manager as configured in the properties files. This
	 * requires retrieval of the implementation specified in the primary
	 * logging config and loading the class definition. The implementation
	 * settings are loaded into the specified namespace, the instance is
	 * initialized and then returned.
	 * 
	 * @param settings the application settings manager
	 * @return the primary log manager for this invocation
	 * @throws InstantiationException if the log manager implementation class
	 * 	cannot be instantiated
	 * @throws IllegalAccessException if there is a problem accessing the
	 * 	log manager implementation
	 * @throws ClassNotFoundException if the log manager implementation class
	 * 	is not found on the class path
	 * @throws FileNotFoundException if the implementation configuration file
	 * 	is not found
	 * @throws IOException if there is a problem reading the configuration file
	 */
	public static LogManager getInstance(Settings settings) throws InstantiationException, IllegalAccessException, ClassNotFoundException, FileNotFoundException, IOException{
		//properly wrap the primary settings manager
		NamespaceSettings logProps = new NamespaceSettings(settings, NAMESPACE);
		
		//retrieve the logging settings
		String managerClass = logProps.getProperty(Manager.MANAGER_CLASS);
		String configFile = logProps.getProperty(Manager.CONFIG_FILE);
		String settingsNamespace = logProps.getProperty(Manager.MANAGER_NAMESPACE);
		
		//retrieve an initialized instance of the log manager
		logManager = Manager.getInstance(settings, managerClass, configFile, settingsNamespace, false);
		
		return logManager;
	}
	
	/**
	 * Retrieve the specified application namespace logger. Each application
	 * component can choose to log to a specific namespace. The implementation
	 * defines how it handles each namespace, however a valid logger must always
	 * be returned. This means that if the specified logging namespace does not
	 * have a corresponding backing definition, the logging messages should
	 * go to a separate log file.
	 * 
	 * @param loggingNamespace the application component for which to retrieve
	 * 	a logger
	 * @return the logger corresponding to that application component
	 */
	public abstract Logger getLogger(String loggingNamespace);
}
